<template>
  <div style="padding: 15px">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" v-model="pressName" placeholder="请输出版社名称"/>
      <Button type="success" @click="searchOrg">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加出版社</Button>
    </div>
    <Table border :columns="pressTitle" :data="pressList"></Table>
    <page-com :total="total" @page-change="pageChange"></page-com>
    <Modal v-model="identity" :title="modalTitle" :width="750" :mask-closable="false"  @on-ok="submit" @on-cancel="cancel">
      <Form :model="addForm" :label-width="80">
        <FormItem label="出版社名称">
          <Input v-model="addForm.pressName" placeholder="请输入身份名称" />
        </FormItem>
        <FormItem label="出版社描述">
          <Input v-model="addForm.description" type="textarea" :autosize="{minRows: 2,maxRows: 3}" placeholder="出版社描述"/>
        </FormItem>
        <FormItem label="设置禁启">
          <i-switch size="large" v-model="addForm.status">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import PageCom from '_c/page'
  const url = '/api/api/book/press/'
  export default {
    name: 'bookPress',
    data () {
      return {
        identity: false,
        modalTitle: '',
        pressName: '',
        number: 0,
        size: 10,
        total: 0,
        addForm: {
          status: true
        },
        pressTitle: [
          {
            title: '出版社名称',
            align: 'center',
            tooltip: true,
            key: 'pressName'
          },
          {
            title: '出版社描述',
            align: 'center',
            tooltip: true,
            key: 'description'
          },
          {
            title: '添加时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', this.$formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '添加人',
            align: 'center',
            tooltip: true,
            key: 'createId'
          },
          {
            title: '更新时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', this.$formatDate(new Date(params.row.updateTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '更改人',
            align: 'center',
            tooltip: true,
            key: 'updateId'
          },
          {
            title: '状态',
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.status === '1' ? 'primary' : row.status === '0' ? 'error' : ''
              const text = row.status === '1' ? '正常' : row.status === '0' ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editor()
                      let row = params.row
                      this.addForm.status = Boolean(row.status === '1' ? 1 : 0)
                      this.addForm.pressName = row.pressName
                      this.addForm.description = row.description
                      this.addForm.id = row.id
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '你确定删除该标签吗？',
                        okText: '确定',
                        cancelText: '取消',
                        onOk: () => {
                          this.remove(params.row.id)
                        }
                      })
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        pressList: []
      }
    },
    mounted() {
      this.prassPgaeList()
    },
    methods: {
      prassPgaeList() {
        this.$get(url + 'page', {
          prassName: this.prassName,
          number: this.number,
          size: this.size
        }).then((res) => {
          if (res.code === 0) {
            this.pressList = res.data
            this.total = res.count
          }
        })
      },
      title(title) {
        this.modalTitle = title
        this.identity = true
      },
      add() {
        this.title('添加分类')
      },
      editor() {
        this.title('编辑分类')
      },
      submit() {
        if (this.modalTitle === '添加分类') {
          this.addOrg()
        } else {
          this.theEditor()
        }
      },
      addOrg() {
        this.addForm.status = Number(this.addForm.status)
        this.$post(url + 'add', this.addForm).then((res) => {
          if (res.code === 200) {
            this.$Message.success('添加成功')
            this.prassPgaeList()
            this.cancel()
          } else {
            this.$Message.error('添加失败')
          }
        }).catch((rel) => {
          this.$Message.error('添加失败')
        })
      },
      theEditor() {
        this.addForm.status = Number(this.addForm.status)
        this.$put(url + 'update', this.addForm).then((res) => {
          if (res.code === 200) {
            this.$Message.success('修改成功')
            this.prassPgaeList()
            this.cancel()
          } else {
            this.$Message.error('修改失败')
          }
        }).catch((rel) => {
          this.$Message.error('修改失败')
        })
      },
      remove(id) {
        this.$deletes(url + 'delete', {
          id: id
        }).then((res) => {
          if (res.code === 200) {
            this.$Message.success('删除成功')
            this.prassPgaeList()
          } else {
            this.$Message.error('删除失败')
          }
        }).catch((rel) => {
          this.$Message.error('删除失败')
        })
      },
      searchOrg() {
        this.prassPgaeList()
      },
      cancel() {
        this.addForm = {}
        this.addForm.status = true
      },
      pageChange(data) {
        this.number = data
        this.prassPgaeList()
      }
    },
    components: {
      PageCom
    }
  }
</script>

<style scoped>

</style>
